package org.mozhu.mboot.admin.system.privilege.entity;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Formula;
import org.mozhu.mboot.core.data.entity.BaseTreeable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.List;

@Entity
@Table(name = "sys_permission")
@Data
@EqualsAndHashCode(callSuper = true)
public class Permission extends BaseTreeable {

    private static final long serialVersionUID = -6872376783695447530L;

    /**
     * 标识，如果有多个，请以;隔开
     */

    private String code;

    /**
     * 描述
     */
    private String description;

    /**
     * 是否是默认权限
     */
    @Column(name = "default_permission")
    private Boolean defaultPermission = Boolean.FALSE;

    /**
     * 是否有叶子节点
     */
    @Formula(value = "(select count(*) from sys_permission f_t where f_t.parent_id = id)")
    private boolean hasChildren;

    public List<String> getPermissionCodes() {
        if (StringUtils.isEmpty(code)) {
            return Lists.newArrayList();
        }
        return Lists.newArrayList(Splitter.on(";").split(code));
    }
}
